package com.woniuxy01;

import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-07-29 9:31
 */
public class Test01 {
    public static void main(String[] args) {
        int left = 0, right = Integer.MAX_VALUE;
        int middle = (right - left) / 2 + left;
        System.out.println(middle);

        left = middle + 1;
        middle = (right - left) / 2 + left;
        System.out.println(middle);
    }

    private static void test03() {
        int left = 0, right = Integer.MAX_VALUE;
        int middle = (left + right) >>> 1;
        System.out.println(middle);

        left = middle + 1;
        middle = (left + right) >>> 1;
        System.out.println(middle);
    }

    private static void test02() {
        int[] array = {1, 2, 3};
        System.out.println(binarySearch(array, 5));
        System.out.println(binarySearch(array, 2));
        System.out.println(binarySearch(array, 1));
        Arrays.binarySearch(array, 5);
    }

    public static int binarySearch(int[] array, int num) {
        int left = 0, right = array.length - 1, middle;
        while (left <= right) {
            //middle = (right + left) / 2;
            middle = (right - left) / 2 + left;

            if (array[middle] == num) {
                return middle;
            } else if (array[middle] > num) {
                right = middle - 1;
            } else {
                left = middle + 1;
            }
        }
        return -1;
    }


    private static void test01() {
        String source = "abc", target = "ahcbgd";
        boolean flag = isSubString(target, source);
        System.out.println(flag);
    }

    private static boolean isSubString(String target, String source) {
        int sourceIndex = 0, targetIndex = 0;
        while (sourceIndex < source.length() && targetIndex < target.length()) {
            if (source.charAt(sourceIndex) == target.charAt(targetIndex)) {
                sourceIndex++;
            }
            targetIndex++;
        }
        //true? false?
//        System.out.println(sourceIndex);
        return sourceIndex == source.length();
    }
}
